package main

import (
	"fmt"
	camunda_client_go "github.com/citilinkru/camunda-client-go/v3"
	"github.com/citilinkru/camunda-client-go/v3/processor"
	"time"
)

func main() {
	//go客户端
	client := camunda_client_go.NewClient(camunda_client_go.ClientOptions{
		EndpointUrl: "http://localhost:8082/engine-rest",
		ApiUser:     "admin",
		ApiPassword: "admin",
		Timeout:     time.Second * 100000,
	})

	logger := func(err error) {
		fmt.Println(err.Error())
	}

	proc := processor.NewProcessor(client, &processor.Options{
		WorkerId:           "go-client",
		LockDuration:       time.Second * 50,
		MaxTasks:           10,
		LongPollingTimeout: 500 * time.Second}, logger)

	//defKey := "process_external_task"
	proc.AddHandler([]*camunda_client_go.QueryFetchAndLockTopic{
		{TopicName: "try_self_repair"},
		//{LockDuration: 1000},
		//{ProcessDefinitionKeyIn: &defKey},
	},

		func(ctx *processor.Context) error {
			fmt.Println("进入到go外部任务")
			time.Sleep(time.Second * 1)
			isFree := ctx.Task.Variables["isFree"].Value.(bool)
			if isFree {
				fmt.Println("免费维修，不尝试自修")
				errMsg := "我是go服务，不尝试自修"
				errDetail := "这里是taskTrace"
				retrys := 0
				retryTimeout := 5000
				ctx.HandleFailure(processor.QueryHandleFailure{
					ErrorMessage: &errMsg,
					ErrorDetails: &errDetail,
					Retries:      &retrys,
					RetryTimeout: &retryTimeout,
				})

			} else {
				fmt.Println("维修收费，我要尝试自修")
				ctx.Complete(processor.QueryComplete{})
			}
			return nil
		},
	)
	//让主进程不退出
	select {}
}
